<?php
	//本月开盘
	//error_reporting(0);
	set_time_limit(E_ALL);
	include 'simplehtmldom/simple_html_dom.php';
	include_once(dirname(__FILE__).'/dbutil.php');
	function saveRecord($table,$record,$db){
    	if(empty($table))
    		return false;
    	if(!is_array($record) || empty($record))
    		return false;
    	//$db = new DBUtil();
    	//$this->initDB();
    	$r = array();
    	foreach($record as $key=>$item){
    		$r[$db->escape($key)] = $db->escape($item);
    	}
    	$cols = implode("," , array_keys($r));
    	$values = implode("','" , $r);
    	$sql = "insert into " . $db->escape($table) . " (" . $cols . ") values ('" . $values . "')";
    	return $db->query($sql) ? $db->getlastid() : false;
    }
	echo '<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />';
	// 房天下
	$url='http://newhouse.sh.fang.com/house/s/';
	$city='上海';
	$baseUrl='http://newhouse.sh.fang.com/';
	while(true){
	//	$recordList = array();
		// 西安房天下
		$listPage = file_get_html('compress.zlib://'.$url);
			if(!$listPage){
				$listPage = file_get_html($url);
			}
	/*	if(empty($listPage)){
			break;
		}*/
//echo $listPage;exit;
	//	$list = $listPage->find('div[class=morecitydown]');
	//别墅上月参考价
/*	$bs=$listPage->find('div[class=listBox]');
	if(!empty($bs)){
		$bs=$bs[1]->find('a');
		$bdata=array();
		foreach($bs as $bi){
			$tes=trim(mb_convert_encoding($bi->innertext,'utf-8','gbk'));
		//	echo $tes;
			if($tes == '找别墅'){
				$burl=$bi->href;
				$sd=file_get_html('compress.zlib://'.$burl);
					if(!$sd){
						$sd = file_get_html($burl);//楼盘首页
					}
				$sd=$sd->find('div[class=newcardR clearfix]');
				$sd=$sd[0]->find('p[class=setNum]');
				$sd=$sd[1]->find('b');
				$bprice=$sd[0]->innertext;
				$time=date('Y-m-d H:i:s');
				$bdata['city']=$city;
				//$bdata['cityId']='';
				$bdata['price']=$bprice;
				$bdata['time']=$time;
				$bdata['month']=date('m',strtotime("$time -1 month"));
				$bdata['year']=date('Y',strtotime("$time -1 month"));
				//保存数据
				$db = new DBUtil();
				$h = $db->getOneRow("select id from t_villa where city = '".$db->escape($bdata['city'])."' and year='".$db->escape($bdata['year'])."' and month ='".$db->escape($bdata['month'])."'");
				if(empty($h)){
					$cid=$h = $db->getOneRow("select c.cityID,c.father from hat_city c left join hat_province p on c.father=p.provinceID where  p.province like'%".$db->escape($bdata['city'])."%' 
					or c.city like'%".$db->escape($bdata['city'])."%'");
					$bdata['cityId']=$cid['cityID'];
					// 保存信息
					saveRecord('t_villa',$bdata,$db);
				}
				$db->close();
				echo '上月别墅参考价保存成功<br>';
			}
		}
	}*/
//	var_dump($bdata);exit;
	//本月开盘，本月入住的数据
	       $list = $listPage->find('a[class=snblue]');
	      // var_dump($list);exit;
		if(!empty($list)){
				$newArray=array();//存放本月开盘的数据
				$ruzhu=array();//存放本月交房的数据
				var_dump($list[1]->href);exit;
				foreach($list as $listItem){
					$tmp=trim(mb_convert_encoding($listItem->innertext,'utf-8','gbk'));
					var_dump($tmp);
					/*if($tmp == '本月开盘'){
						$url=$listItem->href;
						echo $url;exit;
						//楼盘列表页面
						$new=file_get_html('compress.zlib://'.$url);//本月数据
						if(!$new){
							$new = file_get_html($url);//楼盘首页
						}
						var_dump($new);exit;
						$urlArr=array();//各月的链接
						$ulist=$new->find('div[class=timeBox]');
						$ulist=$ulist[0]->find('a');
						var_dump($ulist);exit;
						foreach($ulist as $iu){
							$tt=$iu->find('li');
							$tt=$tt[0]->attr;
							//$tt['data-cur']
							$Arr=array();
						//	if($tt['data-ymonth'] >= $start || $tt['data-ymonth']<= $end){
							if(substr($tt['data-ymonth'],0,4) == '2018' && substr($tt['data-ymonth'],4,2) <= date('m')){
							//	$Arr['url']=$baseUrl.$iu->href;
								$Arr['year']=substr($tt['data-ymonth'],0,4);
								$Arr['month']=substr($tt['data-ymonth'],4,2);
								$urlArr[]=$Arr;
							}
							
						}
					var_dump($urlArr);exit;
						for($a=2;$a < count($urlArr); $a++){
							$year=$urlArr[$a]['year'];//date('Y');//年
							$month=$urlArr[$a]['month'];//date('m');//月
							//	echo $year.$month;
							$nextpage=1;
							do{
								$html =  file_get_html('http://localhost/xin.php?ue='.$year.$month.'&page='.$nextpage);//页面
								//	var_dump($html);exit;
								if($nextpage == 1){
										//下一页
									$lastpage=$html->find('div[class=page]')[0]->find('li[class=fr]')[0]->find('a[class=last]');
									$lastpage=$lastpage[0]->attr;
									$lastpage=$lastpage['data-page'];
									if(empty($lastpage['data-page'])){
										$lastpage=0;
									}
								}
								$nextpage+=1;
								$nlist=$html->find('div[class=nlc_details]');//楼盘
									var_dump($nlist);exit;
								foreach($nlist as $dlist){//具体楼盘
									//售价
									$price=$dlist->find('div[class=nhouse_price]');
									$price=$price[0]->find('span');
									$price=trim(mb_convert_encoding($price[0]->innertext,'utf-8','gbk'));
									//楼盘名称、链接
									$durl=$dlist->find('a[target=_blank]');
									$uu=$durl[0]->href;//链接
									$bname=trim(mb_convert_encoding($durl[0]->innertext,'utf-8','gbk'));//名字

									$lpage = file_get_html('compress.zlib://'.$uu);//楼盘首页
									if(!$lpage){
										$lpage = file_get_html($uu);//楼盘首页
									}
									//楼盘详细
									$dta=$lpage->find('div[class=nav]');
									$dta=$dta[0]->find('a');
									$turl=$dta[1]->href;//楼盘详情页面链接
									$tpage = file_get_html('compress.zlib://'.$turl);//楼盘详情页
									if(!$tpage){
										$tpage = file_get_html($turl);//楼盘首页
									}							
									$binfor=$tpage->find('div[class=main-item]');
									//建筑面积
									$larea=$binfor[3]->find('li');
									$larea=$larea[1]->find('div[class=list-right]');
									$larea=trim(mb_convert_encoding($larea[0]->innertext,'utf-8','gbk'));

									$ub=$binfor[0]->find('ul[class=list clearfix]');
								//楼盘的物业类别，装修状况
									$li=$ub[0]->find('li');
									$lei=$li[0]->find('div[class=list-right]');
									$lei=trim(mb_convert_encoding($lei[0]->innertext,'utf-8','gbk'));//物业类别
									$zx=$li[3]->find('div[class=list-right]');
									$zx=trim(mb_convert_encoding($zx[0]->innertext,'utf-8','gbk'));//物业类别
									if(strpos($zx,'a')){
										$zx=trim(mb_substr($zx,0,strpos($zx,'a')-1));
									}
									$listArray=array();
									$listArray['city']=$city;//楼盘所在城市
								//	$listArray['cityId']=;//城市ID
									$listArray['name']=$bname;//楼盘名字
									$listArray['area']=$larea;//楼盘建筑面积
									$listArray['price']=$price;//售价
									$listArray['renovation']=$zx;//装修情况
									$listArray['type']=$lei;//物业类型
										//根据物业类型进行楼盘分类
									if(strstr($lei,'普通住宅') || strstr($lei,'花园洋房')){
										$listArray['style']=1;
									}elseif(strstr($lei,'别墅')){
										$listArray['style']=2;
									}else{
										$listArray['style']=3;
									}
									$listArray['month']=$month;//数据月份
									$listArray['year']=$year;//数据年份
									$listArray['time']=date('Y-m-d H:i:s');//
									$newArray[]=$listArray;
								//	var_dump($listArray);
								//	exit;
								}
							}while($nextpage <= $lastpage);
						}
				}
			var_dump($newArray);	exit;
			/*
				if($tmp == '本月入住'){
						$url=$listItem->href;
					//	echo $url;
						//楼盘列表页面
						$new=file_get_html('compress.zlib://'.$url);//本月数据
						if(!$new){
							$new = file_get_html($url);//楼盘首页
						}
						$urlArrb=array();//各月的链接
						$ulist=$new->find('div[class=timeBox]');
						$ulist=$ulist[0]->find('a');
						foreach($ulist as $iu){
							$tt=$iu->find('li');
							$tt=$tt[0]->attr;
							$Arr=array();
							if(substr($tt['data-ymonth'],0,4) == '2018' && substr($tt['data-ymonth'],4,2) <= date('m')){
							//	$Arr['url']=$baseUrl.$iu->href;
								$Arr['year']=substr($tt['data-ymonth'],0,4);
								$Arr['month']=substr($tt['data-ymonth'],4,2);
								$urlArrb[]=$Arr;
							}
							
						}
						for($a=0;$a < count($urlArrb); $a++){
							$year=$urlArrb[$a]['year'];
							$month=$urlArrb[$a]['month'];//月
							$nextpage=1;
							do{
								$html =  file_get_html('http://localhost/test-tx2.php?ue='.$year.$month.'&page='.$nextpage);//页面
								if($nextpage == 1){
										//下一页
									$lastpage=$html->find('div[class=page]')[0]->find('li[class=fr]')[0]->find('a[class=last]');
									$lastpage=$lastpage[0]->attr;
									$lastpage=$lastpage['data-page'];
									if(empty($lastpage['data-page'])){
										$lastpage=0;
									}
								}
								$nextpage+=1;
								//楼盘
								$nlist=$html->find('div[class=nlc_details]');
								for($n=0;$n<count($nlist);$n++){
									//售价
									$price=$nlist[$n]->find('div[class=nhouse_price]');
									$price=$price[0]->find('span');
									$price=trim(mb_convert_encoding($price[0]->innertext,'utf-8','gbk'));
									//楼盘名称、链接
									$durl=$nlist[$n]->find('a[target=_blank]');
									$uu=$durl[0]->href;//链接
									$bname=trim(mb_convert_encoding($durl[0]->innertext,'utf-8','gbk'));//名字
									$lpage = file_get_html('compress.zlib://'.$uu);//楼盘首页
									if(!$lpage){
										$lpage = file_get_html($uu);//楼盘首页
									}
									//楼盘详细
									$dta=$lpage->find('div[class=nav]');
									$dta=$dta[0]->find('a');
									$turl=$dta[1]->href;
									$tpage = file_get_html('compress.zlib://'.$turl);//楼盘详情页
									if(!$tpage){
										$tpage = file_get_html($turl);//楼盘首页
									}							
									$binfor=$tpage->find('div[class=main-item]');
									//建筑面积
									$larea=$binfor[3]->find('li');
									$larea=$larea[1]->find('div[class=list-right]');
									$larea=trim(mb_convert_encoding($larea[0]->innertext,'utf-8','gbk'));

									$ub=$binfor[0]->find('ul[class=list clearfix]');
								//楼盘的物业类别，装修状况
									$li=$ub[0]->find('li');
									$lei=$li[0]->find('div[class=list-right]');
									$lei=trim(mb_convert_encoding($lei[0]->innertext,'utf-8','gbk'));//物业类别
									$zx=$li[3]->find('div[class=list-right]');
									$zx=trim(mb_convert_encoding($zx[0]->innertext,'utf-8','gbk'));//物业类别
									if(strpos($zx,'a')){
										$zx=trim(mb_substr($zx,0,strpos($zx,'a')-1));
									}
									
									$listArray=array();
									$listArray['city']=$city;//楼盘所在城市
								//	$listArray['cityId']=;//城市ID
									$listArray['name']=$bname;//楼盘名字
									$listArray['area']=$larea;//楼盘建筑面积
									$listArray['price']=$price;//售价
									$listArray['renovation']=$zx;//装修情况
									$listArray['type']=$lei;//物业类型
										//根据物业类型进行楼盘分类
									if(strstr($lei,'普通住宅') || strstr($lei,'花园洋房')){
										$listArray['style']=1;
									}elseif(strstr($lei,'别墅')){
										$listArray['style']=2;
									}else{
										$listArray['style']=3;
									}
									$listArray['month']=$month;//数据月份
									$listArray['year']=$year;//数据年份
									$listArray['time']=date('Y-m-d H:i:s');//
									$ruzhu[]=$listArray;
								}
							}while($nextpage <= $lastpage);
					}
				}*/
	
			}
		//	var_dump($ruzhu);exit;
			$db = new DBUtil();
			//本月开盘
			$mm=0;
			foreach($newArray as $item){
				$h = $db->getOneRow("select id from t_new_building where city = '".$db->escape($item['city'])."' and year='".$db->escape($item['year'])."' and month ='".$db->escape($item['month'])."' 
				and name='".$db->escape($item['name'])."'");
				if(empty($h)){
					$cid=$h = $db->getOneRow("select c.cityID,c.father from hat_city c left join hat_province p on c.father=p.provinceID where  p.province like'%".$db->escape($item['city'])."%' or c.city like'%".$db->escape($item['city'])."%'");
					$item['cityId']=$cid['cityID'];
					// 保存信息
					saveRecord('t_new_building',$item,$db);
					$mm+=1;
				}
			}
			echo '本月开盘'.count($newArray).'个楼盘<br>';
			echo '本月新盘结束，保存'.$mm.'条<br>';
			//本月入住
			
			$mm=0;
			foreach($ruzhu as $item){
				$h = $db->getOneRow("select id from t_hand_building where city = '".$db->escape($item['city'])."' and year='".$db->escape($item['year'])."' and month ='".$db->escape($item['month'])."' 
				and name='".$db->escape($item['name'])."'");
				if(empty($h)){
					$cid=$h = $db->getOneRow("select c.cityID,c.father from hat_city c left join hat_province p on c.father=p.provinceID where  p.province like'%".$db->escape($item['city'])."%' or c.city like'%".$db->escape($item['city'])."%'");
					$item['cityId']=$cid['cityID'];
					// 保存信息
					saveRecord('t_hand_building',$item,$db);
					$mm+=1;
				}
			}
			echo '本月交房'.count($ruzhu).'个楼盘<br>';
			echo '本月入住结束保存'.$mm.'条<br>';
			$db->close();
			echo 'finish';
			exit;
		}
		/*
		$nextPage = $listPage->find('div[class=page]')[0]->find('a[class=active]')[0]->next_sibling();
		echo ($nextPage->href.'<br/>');
		if(!empty($nextPage) ){
			$url = $baseUrl . $nextPage->href;
		}else{
			break;
		}*/
		//sleep(30);
		//echo $url;break;
	}
	function replace_unicode_escape_sequence($match)  
{         
        return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');
} 
	// 删除html
	function removeHtml($value){
		return preg_replace('/<.*?<\/\w+>/','',$value);
	}
	// 删除全部空格
	function removeSpace($value){
		return preg_replace('/\s+/','',$value);
	}
?>